<template>
	<el-card>
		<div slot="header" class="clearfix">
			<!--工具条-->
			<el-form :inline="true" :model="filters" class="filter-form-box">
				<el-form-item>
					<el-input size="small" v-model="filters.name" placeholder="院系名称"></el-input>
				</el-form-item>
				<el-form-item style="margin-right: 0;">
					<el-button size="mini" v-on:click="refreshFilters"><i class="fa fa-refresh"></i></el-button>
				</el-form-item>
				<el-form-item>
					<el-button size="mini" v-on:click="getData"><i class="fa fa-search"></i></el-button>
				</el-form-item>
				<el-form-item style="float: right">
					<el-button type="primary" size="small" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</div>

		<!--列表-->
		<el-table border stripe :data="datas" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="name" label="院系名称" sortable>
			</el-table-column>
			<el-table-column prop="remark" label="备注" sortable>
			</el-table-column>
			<el-table-column label="操作" width="150">
				<template slot-scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="20" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增/编辑界面-->
		<el-dialog :title="formType+'院系数据'" :visible.sync="formVisible" :close-on-click-modal="false">
			<el-form :model="formData" label-width="80px" :rules="formRules" ref="formData">
				<el-form-item label="院系名" prop="name">
					<el-input v-model="formData.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="备注">
					<el-input type="textarea" v-model="formData.remark"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click.native="formVisible = false">取消</el-button>
				<el-button type="primary" size="small" @click.native="saveSubmit(1)" :loading="saveLoading" v-show="formType === '新增'">新增</el-button>
				<el-button type="primary" size="small" @click.native="saveSubmit(2)" :loading="saveLoading" v-show="formType === '修改'">保存</el-button>
			</div>
		</el-dialog>
	</el-card>
</template>

<script>
  import { getDepartmentList, removeDepartmentData, saveDepartmentData } from '../../api/api';

  export default {
    data() {
      return {
        filters: {
          name: ''
        },
        datas: [],
        total: 0,
        page: 0,
        listLoading: false,
        sels: [],//列表选中列
        formType:'',
        formVisible: false,//新增界面是否显示
        saveLoading: false,
        formRules: {
          name: [
            { required: true, message: '请输入院系名', trigger: 'blur' }
          ]
        },
        //新增界面数据
        formData: {
          name: '',
          remark: ''
        }
      }
    },
    methods: {
      handleCurrentChange(val) {
        this.page = val;
        this.getData();
      },

      refreshFilters(){
        this.filters = {
          name: ''
        };
        this.getData();
      },

      //获取数据列表
      getData() {
        let para = {
          count: 10,
          pageNo: this.page,
          sortDirection: 'asc',
          sortField: ["name"],
		  name: this.filters.name,
        };
        this.listLoading = true;
        getDepartmentList(para).then((res) => {
          this.total = res.data.data.totalPages;
          this.datas = res.data.data.content;
          this.listLoading = false;
        });
      },
      //删除
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          let para = { id: row.id };
          removeDepartmentData(para).then((res) => {
            this.listLoading = false;
            this.$message({
              message: '删除成功',
              type: 'success'
            });
            this.getData();
          });
        }).catch(() => {

        });
      },
      //显示编辑界面
      handleEdit: function (index, row) {
        this.formType = '修改';
        this.formVisible = true;
        this.formData = Object.assign({}, row);
      },
      //显示新增界面
      handleAdd: function () {
        this.formType = '新增';
        this.formVisible = true;
        this.formData = {
          name: '',
          remark: ''
        };
      },
      //新增
      saveSubmit: function (num) {
        this.$refs.formData.validate((valid) => {
          if (valid) {
            let user = sessionStorage.getItem('user');
            if (user) {
              user = JSON.parse(user);
              let T_msg = '';
              let jsonStr = [];
              if(num === 1){
                jsonStr = {
                  authorUserId: '',
                  name: '',
                  remark: ''
                };
                jsonStr.name = this.formData.name;
                jsonStr.remark = this.formData.remark;
                jsonStr.authorUserId = user.id;
                T_msg = '确认添加吗？';
              }else if(num === 2){
                jsonStr = Object.assign({}, this.formData);
                T_msg = '确认修改吗？';
              }
              this.$confirm(T_msg, '提示', {}).then(() => {
                this.saveLoading = true;
                saveDepartmentData(jsonStr).then((res) => {
                  this.saveLoading = false;
                  //NProgress.done();
                  this.$message({
                    message: '提交成功',
                    type: 'success'
                  });
                  this.$refs['formData'].resetFields();
                  this.formVisible = false;
                  this.refreshFilters();
                });
              });
            }
          }
        });
      },
      selsChange: function (sels) {
        this.sels = sels;
      },
    },
    mounted() {
      this.getData();
    }
  }

</script>

<style scoped>

</style>